package com.example.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entry.RiderEvaluate;
import com.example.service.IRiderEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 骑手评价后台管理控制器
 * */
@RequestMapping("/admin/rider-evaluate")
@RestController
public class RiderEvaluateController {

    @Autowired
    private IRiderEvaluateService riderEvaluateService;

    /*
     * 根据id查询信息
     * */
    @GetMapping("/rider-evaluate-id={id}")
    public Result getRiderEvaluateOne(@PathVariable Integer id){
        RiderEvaluate byId = riderEvaluateService.getById(id);
        return Result.success(byId);
    }

    /*
     * 根据id进行删除
     * */
    @GetMapping("/delete/id={id}")
    public Result deleteRiderEvaluateInfo(@PathVariable Integer id) {
        riderEvaluateService.removeById(id);
        return Result.success("删除成功");
    }

    /*
     * 批量删除
     * */
    @PostMapping("/batchd_delete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        riderEvaluateService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }

    /*
     * 修改或保存信息
     * */
    @PostMapping("/update")
    public Result updateOrSavRiderEvaluate(@RequestBody RiderEvaluate riderEvaluate){
        riderEvaluateService.saveOrUpdate(riderEvaluate);
        return Result.success("更新成功");
    }

    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public Result getPageRiderEvaluate(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String riderName,
                                 @RequestParam(defaultValue = "") String userName){
        QueryWrapper<RiderEvaluate> queryWrapper = new QueryWrapper<>();
        // 根据id进行降序排序
        queryWrapper.orderByDesc("id");
        // 根据条件进行模糊查询
        if(!"".equals(riderName)){
            queryWrapper.like("rider_name", riderName);
        }
        if(!"".equals(userName)){
            queryWrapper.like("user_name", userName);
        }
        // 分页构造器
        Page<RiderEvaluate> page = riderEvaluateService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}
